package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;


    @Override
    public List<NursingLevelVo> listAll() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.listAll();
        return nursingLevels.stream().map(nursingLevel -> {
            NursingLevelVo nursingLevelVo = new NursingLevelVo();
            BeanUtils.copyProperties(nursingLevel, nursingLevelVo);
            return nursingLevelVo;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void addNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        try {
            nursingLevelMapper.insert(nursingLevel);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.DATA_CANNOT_DUPLICATE);
        }

    }

    @Override
    public PageResponse selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        //设置分页
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingLevelVo> nursingLevel = nursingLevelMapper.selectByPage(name, status);
        return PageResponse.of(nursingLevel, NursingLevelVo.class);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo findById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.findById(id);
        return BeanUtil.toBean(nursingLevel, NursingLevelVo.class);

    }

    /**
     * 修改护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 删除护理等级
     */
    @Override
    public void deleteById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.findById(id);
        if(ObjectUtil.isNotEmpty(nursingLevel) && nursingLevel.getStatus().equals(1)){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingLevelMapper.deleteById(id);
    }

    /**
     * 启用或禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void isEnable(Long id, Integer status) {
        nursingLevelMapper.isEnable(id,status);
    }
}
